<template>
  <div>
    <!-- 搜索部分 -->
    <el-card shadow="never" class="mb-5">
      <div class="flex">
        <!-- 分类名称搜哟 -->
        <el-text>分类名称</el-text>
        <div class="ml-3 w-52 mr-5">
          <el-input v-model="searchCategoryName" placeholder="请输入（模糊查询）" @keyup.enter="queryCategoryList"/>
        </div>
        <!-- 日期搜索 -->
        <el-text>创建日期</el-text>
        <div class="ml-3 w-30 mr-5">
          <div class="block">
            <el-date-picker
                v-model="pickDate"
                type="daterange"
                unlink-panels
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :shortcuts="shortcuts"
                size="default"
                value-format="YYYY-MM-DD"
            />
          </div>
        </div>
        <!-- 功能按钮-->
        <el-button type="primary" class="ml-3" :icon="Search" @click="queryCategoryList">查询</el-button>
        <el-button class="ml-3" :icon="RefreshRight" @click="resettingQueryValue">重置</el-button>
      </div>
    </el-card>

    <!-- 内容展示部分 -->
    <el-card shadow="never">
      <el-button type="primary" class="mt-1 mb-5" :icon="Plus" @click="dialogVisible = true">新增分类</el-button>
      <el-table :data="tableData" border style="width: 100%" v-loading="tableLoading" >
        <!--        <el-table-column prop="date" label="序号" width="180"/>-->
        <el-table-column prop="name" label="分类名称" width="180"/>
        <!--        <el-table-column prop="address" label="文章数" width="180"/>-->
        <el-table-column prop="createTime" label="创建时间" width="200"/>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="danger" size="small" @click="delCategory(scope.row)">
              <el-icon class="mr-1">
                <Delete/>
              </el-icon>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="current"
          v-model:page-size="size"
          :page-sizes="[10, 20, 50]"
          :small="false"
          :disabled="false"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          class="mt-10 flex items-center justify-center"
      />
    </el-card>

    <!-- 新增分类弹出框 -->
    <el-dialog
        v-model="dialogVisible"
        title="新增分类"
        width="500"
        draggable
        overflow
    >
      <el-input
          v-model="newCategoryName"
          maxlength="10"
          placeholder="请输入分类名"
          show-word-limit
          type="text"
      />
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addCategory" :loading="btnLoading">
            新增
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {Search, RefreshRight, Delete, Plus} from '@element-plus/icons-vue'
import {ref, onMounted} from "vue";
import {categoryAdd, categoryDelete, categoryQueryByPage, categoryQueryList} from "@/api/admin/category.js"
import {showNotification, showModel} from "@/composables/util.js"

// 分页查询的分类名称
const searchCategoryName = ref("")
// 选择的日期
const pickDate = ref("")
const shortcuts = [
  {
    text: '最近一周',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    },
  },
  {
    text: '最近一个月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      return [start, end]
    },
  },
  {
    text: '最近三个月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
      return [start, end]
    },
  },
]
const tableData = ref([])
// 当前页码，给了一个默认值 1
const current = ref(1)
// 总数据量，给了个默认值 0
const total = ref(0)
// 每页显示的数据量，给了个默认值 10
const size = ref(10)
const dialogVisible = ref(false)
const newCategoryName = ref('')
const tableLoading = ref(false)
const btnLoading = ref(false)
onMounted(() => {
  queryCategoryList()
})

const queryCategoryList = () => {
  tableLoading.value = true
  // 封装参数
  const data = {
    "current": current.value,
    "size": size.value,
    "name": searchCategoryName.value,
    "startDate": "",
    "endDate": ""
  }
  if (pickDate.value) {
    data.startDate = pickDate.value[0]
    data.endDate = pickDate.value[1]
  }
  // 发送请求
  categoryQueryByPage(data).then(resp => {
    if (resp.success) {
      tableData.value = resp.data
      total.value = resp.total
      size.value = resp.size
      current.value = resp.current
    }
  }).finally(() => {
    tableLoading.value = false
  })
}

// 重置查询接口
const resettingQueryValue = () => {
  searchCategoryName.value = ''
  pickDate.value = ''
}

// 新增分类
const addCategory = () => {
  btnLoading.value = true
  categoryAdd({"name": newCategoryName.value}).then(resp => {
    if (resp.success) {
      showNotification("新增分类", "新增分类成功！")
      queryCategoryList()
    } else {
      showNotification("新增分类", resp.message, "warning")
    }
  }).finally(() => {
    // 关闭弹出框
    btnLoading.value = false
    dialogVisible.value = false
  })
}

// 删除分类接口
const delCategory = (row) => {
  showModel("确定删除该分类吗？", "删除分类").then(() => {
    categoryDelete(row.id).then(resp => {
      if (resp.success) {
        showNotification("删除分类", "删除成功")
        queryCategoryList()
      } else {
        showNotification("删除分类", resp.message, "warning")
      }
    })
  })
}


// 分页
const handleSizeChange = (value) => {
  size.value = value
  queryCategoryList()
}
const handleCurrentChange = (item) => {
  current.value = item
  queryCategoryList()
}
</script>

<style scoped>

</style>